<?php 
      
       
      class ajax extends model  { 

         private $arraydata=array();
      
           function __construct()
           { 
            
             global $db_exec,$info,$memcache;
             $this->db=$db_exec;   
             $this->db->get_connect();
             $this->info=$info; 
             $this->memcache=$memcache;
             //echo "ddd";
            
             if($_POST)
             {
             $this->post=$_POST;
             }
             if($_GET)
             {
             $this->get=$_GET;
             }
             
             
             
             
             if($this->get['nojson']!=1)
             {
             header('Content-type: application/json');
             }
             
             
             if($_COOKIE['oauth'])
             {
             $this->userdata=$_COOKIE;
             } 
             
             
             
             if($_GET['oauth'])
             {
             
             $this->userdata=$this->getuserbyoauth($_GET['oauth']);

             
             }
             
             
             if($_POST['oauth'])
             {
             $this->userdata=$this->getuserbyoauth($_POST['oauth']);
             
             
             } 
             
             if(method_exists($this,$this->info['parameter'][0]))
             {
              eval("\$this->".$this->info['parameter'][0]."();");     
                 
             }else
             {
                $this->arraydata['error'][]=2;
                
             }
                 
                                    
           
           }
           
           function login()
           {
           if($this->post['facebookid']&&$this->post['stringdata'])
           {
           $sql='select * from tb_user where tb_user.facebookid="'.$this->post['facebookid'].'" and tb_user.status=1';
           $arraycheck=$this->db->db_set_1recordrow($sql);
           
           if($arraycheck['userid'])
           {
           
                        
           
           
           
           }else
           {
           
           
           $objdatauser=json_decode($this->post['stringdata']);
           $arraydatauser=object_to_array($objdatauser);
           $arraydata=array();
           $arraydata['facebookid']=$arraydatauser['facebookid'];
           $arraydata['roleid']=1;
           $arraydata['status']=1;
           $arraydata['name']=$arraydatauser['name'];
           $arraydata['first_name']=$arraydatauser['first_name'];
           $arraydata['last_name']=$arraydatauser['last_name'];
           $arraydata['link']=$arraydatauser['link'];
           if($arraydatauser['hometown'])
           {
           $arraydata['hometown']=$arraydatauser['hometown']['name'];
           }
           
           if($arraydatauser['location'])
           {
           $arraydata['location']=$arraydatauser['location']['name'];
           }
           
           
           $arraydata['gender']=$arraydatauser['gender'];
           $arraydata['timezone']=$arraydatauser['timezone'];
           $arraydata['locale']=$arraydatauser['locale'];
           $arraydata['verified']=$arraydatauser['verified'];
           $arraydata['updated_time']=$arraydatauser['updated_time'];
           $arraydata['username']=$arraydatauser['username'];
           $arraydata['createdate']=date("Y-m-d H:i:s");
           $arraydata['oauth']=md5($arraydatauser['name'].time().rand(1,50000000));
           $this->db->db_set($arraydata,'tb_user');
           $userid=$this->db->db_get_last_number();
           
           if(count($arraydatauser['work'])&&isset($arraydatauser['work']))
           {
           foreach($arraydatauser['work'] as $valuedata)
           {
           
           
           if(isset($valuedata['employer'])&&isset($valuedata['position']))
           {
           $arraydata=array();
           $arraydata['userid']=$userid;
           $arraydata['employer']=$valuedata['employer']['name'];
           $arraydata['position']=$valuedata['position']['name'];
           $arraydata['start_date']=$valuedata['start_date'];
           $this->db->db_set($arraydata,'tb_work');
           }
           
           }
           
           
           
           }
           
           
           
           
           if(count($arraydatauser['education'])&&isset($arraydatauser['education']))
           {
           foreach($arraydatauser['education'] as $valuedata)
           {
           
           
           if(isset($valuedata['school'])&&isset($valuedata['type']))
           {
           
           $arraydata=array();
           $arraydata['userid']=$userid;
           $arraydata['school']=$valuedata['school']['name'];
           $arraydata['type']=$valuedata['type'];
           $this->db->db_set($arraydata,'tb_education');
           
           }
           
           }
           
           
           
           }
           
           
           
           
           $arraycheck['oauth']=$arraydata['oauth'];
           $arraycheck['facebookid']=$arraydata['facebookid'];
           $arraycheck['name']=$arraydatauser['name'];

           
           }
           

          // $this->arraydata['data']['oauth']=;
           
           
           
           
           }
           else if($this->post['oauth'])
           {
           $sql='select * from tb_user where tb_user.facebookid="'.$this->post['oauth'].'" and tb_user.status=1';
           $arraycheck=$this->db->db_set_1recordrow($sql);
           
           }
           
           
           if($arraycheck['name'])
           {
           $arraycheck2['name']=$arraycheck['name'];
           $arraycheck2['oauth']=$arraycheck['oauth'];
           $arraycheck2['facebookid']=$arraycheck['facebookid'];
           $arraycheck2['pic']='https://graph.facebook.com/'.$arraycheck2['facebookid'].'/picture';
           
           if($arraycheck2['oauth'])
           {
           foreach($arraycheck2 as $keycheck => $valuecheck)
           {
           
           setcookie($keycheck,$valuecheck, 0, "/");
           $this->arraydata['data'][$keycheck]=$valuecheck;
           
           }
           
           }
           
           }
           
         //  $this->arraydata['data']=array();
           
           
           }
           
           function checktestpostdata()
           {

            
            if($this->post&&is_array($this->post))
            {
            $this->arraydata['data']=$this->post;
            }else
            {
            
            $this->arraydata['data']=$this->get;
            }
            
            
            
            }
            
            function savescoredata()
            {
            
            if($this->post['apikey']&&$this->post['score']&&$this->post['point']&&$this->post['oauth'])
            {
            if($this->post['apikey']==apikey)
            {
            
            $sql='select userid from tb_user where tb_user.facebookid="'.$this->post['oauth'].'" and tb_user.status=1';
            $arraycheck=$this->db->db_set_1recordrow($sql);
            if($arraycheck['userid'])
            {
            $arraydata=array();
            $arraydata['score']=$this->post['score'];
            $arraydata['point']=$this->post['point'];
            $arraydata['userid']=$arraycheck['userid'];
            $this->db->db_set($arraydata,'tb_scoregame',' userid='.$arraydata['userid']);
            $this->arraydata['data']['success']=1;
            
            }else
            {
            $this->arraydata['data']['success']=0;
            }
            }else
            {
            
            $this->arraydata['data']['success']=0;
            }
            
            
            
            
            }else
            {
            
            $this->arraydata['data']['success']=0;
            
            }
            
            
            
            }
            
            function getquestion()
            {
             if($this->get['apikey']&&$this->get['oauth'])
            {
            if($this->get['apikey']==apikey)
            {
            
            $sql='select userid from tb_user where tb_user.facebookid="'.$this->get['oauth'].'" and tb_user.status=1';
            $arraycheck=$this->db->db_set_1recordrow($sql);
            if($arraycheck['userid'])
            {
            $sql='select 
            tb_question.question,
            tb_question.choice1,
            tb_question.choice2,
            tb_question.choice3,
            tb_question.choice4,
            tb_question.ans,
            tb_question.questionid
            from tb_question';
            $arraydata=$this->db->db_set_recordset($sql);
            $this->arraydata['data']=$arraydata;
            
            }
            
            
            
            
            
            
            }
            
            }
            
            
            
            }
            
            
            
            
            function __destruct() 
           {
             
              
              
              if(count($this->db->db_get_error()))
              {
                 
                //$sqlerror=join("|",$this->db->db_get_error());
                $this->arraydata['error'][]=1;//$this->db->db_get_error();
                $this->arraydata['error']['response']=1;
                
                
              }else if($this->arraydata['error']=="")
              {
                $this->arraydata['error']['response']=0;
              }else
              {
              
                 $this->arraydata['error']['response']=1;
              }
              
              if($this->arraydata['error']['response']==1)
              {
               if(count($this->arraydata['error'])&&is_array($this->arraydata['error']))
               {
               $error=array();

               foreach($this->arraydata['error'] as $key=> $valueerror)
               {
               

               if(is_numeric($key))
               {
                $error[]=$this->geterror($valueerror);
               
               

               
               }
               
               
               
               
               
               }
               
               
               }
              $this->arraydata['error']=array();
              $this->arraydata['error']['response']=count($error);
              $this->arraydata['error']['error']=$error;
              
              
              }
              
              if(!$this->get['nojson'])
              {
              echo   json_encode($this->arraydata);
              }  
              
               $this->db->closedb(); 
            }
           
           
           
      }
?>
